import '../../../constants/enums/api_status.dart';

/// API响应模型，用于封装API请求的响应结果
class ApiResponse<T> {
  /// 响应状态
  final ApiStatus status;
  
  /// 响应数据
  final T? data;
  
  /// 响应消息
  final String? message;
  
  /// HTTP状态码
  final int? statusCode;

  /// 构造函数
  ApiResponse({
    required this.status,
    this.data,
    this.message,
    this.statusCode,
  });

  /// 创建成功响应
  factory ApiResponse.success({
    T? data,
    String? message,
    int statusCode = 200,
  }) {
    return ApiResponse<T>(
      status: ApiStatus.success,
      data: data,
      message: message ?? '操作成功',
      statusCode: statusCode,
    );
  }

  /// 创建错误响应
  factory ApiResponse.error({
    required ApiStatus status,
    String? message,
    int? statusCode,
    T? data,
  }) {
    return ApiResponse<T>(
      status: status,
      data: data,
      message: message ?? '操作失败',
      statusCode: statusCode,
    );
  }

  /// 检查响应是否成功
  bool get isSuccess => status == ApiStatus.success;
  
  /// 检查响应是否失败
  bool get isError => status != ApiStatus.success;
  
  /// 从JSON创建API响应（适用于标准API响应格式）
  factory ApiResponse.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic json)? fromJsonT,
  ) {
    final bool success = json['success'] ?? false;
    final dynamic data = json['data'];
    final String? message = json['message'] as String?;
    final int? code = json['code'] as int?;
    
    if (success) {
      return ApiResponse.success(
        data: fromJsonT != null && data != null ? fromJsonT(data) : data as T?,
        message: message,
        statusCode: code ?? 200,
      );
    } else {
      return ApiResponse.error(
        status: _getStatusFromCode(code),
        message: message,
        statusCode: code,
        data: fromJsonT != null && data != null ? fromJsonT(data) : data as T?,
      );
    }
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson(dynamic Function(T data)? toJsonT) {
    return {
      'success': isSuccess,
      'message': message,
      'code': statusCode,
      'data': data != null && toJsonT != null ? toJsonT(data as T) : data,
    };
  }
  
  /// 根据状态码获取API状态
  static ApiStatus _getStatusFromCode(int? code) {
    if (code == null) return ApiStatus.failed;
    
    switch (code) {
      case 400:
        return ApiStatus.badRequest;
      case 401:
        return ApiStatus.unauthorized;
      case 403:
        return ApiStatus.forbidden;
      case 404:
        return ApiStatus.notFound;
      case 408:
      case 504:
        return ApiStatus.timeout;
      case 500:
      case 501:
      case 502:
      case 503:
        return ApiStatus.serverError;
      default:
        return ApiStatus.failed;
    }
  }
  
  /// 创建一个新的API响应，复制原有属性并覆盖指定属性
  ApiResponse<T> copyWith({
    ApiStatus? status,
    T? data,
    String? message,
    int? statusCode,
  }) {
    return ApiResponse<T>(
      status: status ?? this.status,
      data: data ?? this.data,
      message: message ?? this.message,
      statusCode: statusCode ?? this.statusCode,
    );
  }
} 